using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity; 

public partial class Admin_Setting_Setting : System.Web.UI.UserControl
{
     
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage(); 
        if (!IsPostBack)
        {
            this.ShowMess();
            this.BindDataToLanguageDropDown();
            this.SetLabelLanguage();
            this.SetValidate();
            this.BindData();
        }
    }

    private void ShowMess()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;

        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaultLanguage]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    private void SetValidate()
    {
        int nCurrentLanguage = Support.GetDefaultLanguage() - 1;
        rfvCompanyName.ErrorMessage = MessagesValidate.Setting_COMPANYNAME_EMPTY[nCurrentLanguage];
        rfvAddress.ErrorMessage = MessagesValidate.Setting_ADDRESS_EMPTY[nCurrentLanguage];
        revEmail.ErrorMessage = MessagesValidate.Setting_EMAIL_WRONG[nCurrentLanguage];
        revBranchEmail.ErrorMessage = MessagesValidate.Setting_BRANCHEMAIL_WRONG[nCurrentLanguage];
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;

        lblCompanyName.Text = MultipleLanguage.LBL_COMPANY [nDefaultLanguage];
        lblAddress.Text = MultipleLanguage.LBL_ADDRESS[nDefaultLanguage];
        lblHotline.Text = MultipleLanguage.LBL_HOTLINE[nDefaultLanguage];
        lblTel.Text = MultipleLanguage.LBL_TEL[nDefaultLanguage];
        lblFax.Text = MultipleLanguage.LBL_FAX[nDefaultLanguage];
        lblEmail.Text = MultipleLanguage.LBL_EMAIL[nDefaultLanguage];

        lblBranchCompanyName.Text = MultipleLanguage.LBL_BranchCompanyName[nDefaultLanguage];
        lblBranchAddress.Text = MultipleLanguage.LBL_BranchAddress[nDefaultLanguage];
        lblBranchEmail.Text = MultipleLanguage.LBL_BranchEmail[nDefaultLanguage];
        lblBranchFax.Text = MultipleLanguage.LBL_BranchFax[nDefaultLanguage];
        lblBranchHotline.Text = MultipleLanguage.LBL_BranchHotline[nDefaultLanguage];
        lblBranchTel.Text = MultipleLanguage.LBL_BranchTel[nDefaultLanguage];

        lblYahoo1.Text = MultipleLanguage.LBL_Yahoo1[nDefaultLanguage];
        lblYahoo2.Text = MultipleLanguage.LBL_Yahoo2[nDefaultLanguage];
        lblYahoo3.Text = MultipleLanguage.LBL_Yahoo3[nDefaultLanguage];
        lblYahoo4.Text = MultipleLanguage.LBL_Yahoo4[nDefaultLanguage];

        lblSkype1.Text = MultipleLanguage.LBL_Skype1[nDefaultLanguage];
        lblSkype2.Text = MultipleLanguage.LBL_Skype2[nDefaultLanguage];
        lblSkype3.Text = MultipleLanguage.LBL_Skype3[nDefaultLanguage];
        lblSkype4.Text = MultipleLanguage.LBL_Skype4[nDefaultLanguage]; 

        
        lblDefaultLanguage.Text = MultipleLanguage.LBL_DEFAULT_LANGUAGE [nDefaultLanguage];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage];
        lblFotter.Text = MultipleLanguage.LBL_FOOTER[nDefaultLanguage];
        lblMetaDescription.Text = MultipleLanguage.LBL_META_DESCRIPTION[nDefaultLanguage];
        lblMetaKeyword.Text = MultipleLanguage.LBL_META_KEYWORD [nDefaultLanguage];
        lblWebsite.Text = MultipleLanguage.LBL_WEBSITE [nDefaultLanguage];
        //btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage];
        lnkbtSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage]; 

    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
       
        // Bind Data to Default Language
        ddlDefaultLanguage.DataSource = dtbLanguage;
        ddlDefaultLanguage.DataValueField = "LanguageID";
        ddlDefaultLanguage.DataTextField = "Description";
        ddlDefaultLanguage.DataBind();
        ddlDefaultLanguage.SelectedValue = ConvertData.ConvertToString(Support.GetDefaultLanguage());

        // Bind Data To Language
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }

    private void BindData()
    {
        try
        {
            // Load Setting Data
            Setting objSetting = new Setting();
            DataTable dtbSetting = new DataTable();
            dtbSetting = objSetting.Search();
            int nSettingCount = dtbSetting.Rows.Count;
            int nSettingLastestID = ConvertData.ConvertToInt(  dtbSetting.Rows[nSettingCount-1]["SettingID"]);
            objSetting.LoadById(nSettingLastestID);
            txtMetaKeyword.Text = objSetting.Data.MetaKeyword;
            txtMetaDescription.Text = objSetting.Data.MetaDescription;

            Company objCompany = new Company();
            DataTable dtbCompany = new DataTable();
            dtbCompany = objCompany.Search();
            int nCompanyCount = dtbCompany.Rows.Count;
            int nCompanyLastestID =ConvertData.ConvertToInt( dtbCompany.Rows[nCompanyCount - 1]["CompanyID"]);
            objCompany.LoadById(nCompanyLastestID);

            int nLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
            switch (nLanguage)
            {
                case 1:
                    txtAddress.Text = objCompany.Data.Address1;
                    txtCompanyName.Text = objCompany.Data.CompanyName1;
                    txtBranchAddress.Text = objCompany.Data.BranchAddress1 ;
                    txtBranchCompanyName.Text = objCompany.Data.BranchCompanyName1;
                    break;
                case 2:
                    txtAddress.Text = objCompany.Data.Address2;
                    txtCompanyName.Text = objCompany.Data.CompanyName2;
                    txtBranchCompanyName.Text = objCompany.Data.BranchCompanyName2;
                    txtBranchAddress.Text = objCompany.Data.BranchAddress2;
                    break;
                case 3:
                    txtAddress.Text = objCompany.Data.Address3;
                    txtCompanyName.Text = objCompany.Data.CompanyName3;
                    txtBranchAddress.Text = objCompany.Data.BranchAddress3;
                    txtBranchCompanyName.Text = objCompany.Data.BranchCompanyName3;
                    break;
            }
            txtEmail.Text = objCompany.Data.Email;
            txtFax.Text = objCompany.Data.Fax;
            txtFooter.Text = objCompany.Data.Copyright;
            txtHotline.Text = objCompany.Data.Hotline;
            txtTel.Text = objCompany.Data.Tel;
            txtWebsite.Text = objCompany.Data.Website;
            txtBranchFax.Text = objCompany.Data.BranchFax;
            txtBranchHotline.Text = objCompany.Data.BranchHotline;
            txtBranchTel.Text = objCompany.Data.BranchTel;
            txtBranchEmail.Text = objCompany.Data.BranchEmail;
            txtYahoo1.Text = objCompany.Data.YahooAccount1;
            txtYahoo2.Text = objCompany.Data.YahooAccount2;
            txtYahoo3.Text = objCompany.Data.YahooAccount3;
            txtYahoo4.Text = objCompany.Data.YahooAccount4;
            txtSkype1.Text = objCompany.Data.SkypeAccount1;
            txtSkype2.Text = objCompany.Data.SkypeAccount2;
            txtSkype3.Text = objCompany.Data.SkypeAccount3;
            txtSkype4.Text = objCompany.Data.SkypeAccount4;
        }
        catch
        {
            string sURL = Config.ROOT + Pages.BackEnds.ADMIN;
            Response.Redirect(sURL);
        }
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);  
        // Updating Setting Data
        Setting objSetting = new Setting();
        DataTable dtbSetting = new DataTable();
        dtbSetting = objSetting.Search();
        int nSettingCount = dtbSetting.Rows.Count;
        int nSettingLastestID = ConvertData.ConvertToInt(dtbSetting.Rows[nSettingCount - 1]["SettingID"]);
        objSetting.Data.MetaKeyword = txtMetaKeyword.Text.Trim();
        objSetting.Data.MetaDescription = txtMetaDescription.Text.Trim();
        int nSettingResult = objSetting.Update(nSettingLastestID);

        // Updating Company Data
        Company objCompany = new Company();
        DataTable dtbCompany = new DataTable();
        dtbCompany = objCompany.Search();
        int nCompanyCount = dtbCompany.Rows.Count;
        int nCompanyLastestID = ConvertData.ConvertToInt(dtbCompany.Rows[nCompanyCount - 1]["CompanyID"]);
        eCompany eCompanyEntity = this.CompanyEntity(nCompanyLastestID);

        objCompany.Data.Website =  txtWebsite.Text.Trim();
        objCompany.Data.Tel = txtTel.Text.Trim();
        objCompany.Data.Hotline = txtHotline.Text.Trim();
        objCompany.Data.Fax = txtFax.Text.Trim();
        objCompany.Data.Email = txtEmail.Text.Trim();
        objCompany.Data.Copyright = txtFooter.Text.Trim();
        objCompany.Data.BranchEmail = txtBranchEmail.Text.Trim();
        objCompany.Data.BranchFax = txtBranchFax.Text.Trim();
        objCompany.Data.BranchHotline = txtBranchHotline.Text.Trim();
        objCompany.Data.BranchTel = txtBranchTel.Text.Trim();
        objCompany.Data.YahooAccount1 = txtYahoo1.Text.Trim();
        objCompany.Data.YahooAccount2 = txtYahoo2.Text.Trim();
        objCompany.Data.YahooAccount3 = txtYahoo3.Text.Trim();
        objCompany.Data.YahooAccount4= txtYahoo4.Text.Trim();
        objCompany.Data.SkypeAccount1 = txtSkype1.Text.Trim();
        objCompany.Data.SkypeAccount2 = txtSkype2.Text.Trim();
        objCompany.Data.SkypeAccount3 = txtSkype3.Text.Trim();
        objCompany.Data.SkypeAccount4 = txtSkype4.Text.Trim();


        switch (nLanguage)
        { 
            case 1:
                objCompany.Data.CompanyName1 = txtCompanyName.Text.Trim();
                objCompany.Data.Address1 = txtAddress.Text.Trim();
                objCompany.Data.BranchAddress1  = txtBranchAddress.Text.Trim();
                objCompany.Data.BranchCompanyName1 = txtBranchCompanyName.Text.Trim();   
                
                objCompany.Data.CompanyName2 = ConvertData.ConvertToString(eCompanyEntity.CompanyName2);
                objCompany.Data.Address2 = ConvertData.ConvertToString(eCompanyEntity.Address2) ;
                objCompany.Data.BranchAddress2  = ConvertData.ConvertToString(eCompanyEntity.BranchAddress2 );
                objCompany.Data.BranchCompanyName2 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName2 );
                
                objCompany.Data.CompanyName3 = ConvertData.ConvertToString(eCompanyEntity.CompanyName3 );
                objCompany.Data.Address3 = ConvertData.ConvertToString(eCompanyEntity.Address3);
                objCompany.Data.BranchAddress3  = ConvertData.ConvertToString(eCompanyEntity.BranchAddress3 );
                objCompany.Data.BranchCompanyName3 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName3);
                
                break;
            case 2:
                objCompany.Data.CompanyName2 = txtCompanyName.Text.Trim();
                objCompany.Data.Address2 =  txtAddress.Text.Trim();
                objCompany.Data.BranchAddress2  =  txtBranchAddress.Text.Trim();
                objCompany.Data.BranchCompanyName2 = txtBranchCompanyName.Text.Trim();  

                objCompany.Data.CompanyName1 = ConvertData.ConvertToString(eCompanyEntity.CompanyName1);
                objCompany.Data.Address1 = ConvertData.ConvertToString(eCompanyEntity.Address1);
                objCompany.Data.BranchAddress1 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress1);
                objCompany.Data.BranchCompanyName1 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName1);

                objCompany.Data.CompanyName3 = ConvertData.ConvertToString(eCompanyEntity.CompanyName3);
                objCompany.Data.Address3 = ConvertData.ConvertToString(eCompanyEntity.Address3);
                objCompany.Data.BranchAddress3 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress3);
                objCompany.Data.BranchCompanyName3 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName3);

                break;
            case 3:
                objCompany.Data.CompanyName3 = txtCompanyName.Text.Trim();
                objCompany.Data.Address3 = txtAddress.Text.Trim();
                objCompany.Data.BranchAddress3 =  txtBranchAddress.Text.Trim();
                objCompany.Data.BranchCompanyName3 = txtBranchCompanyName.Text.Trim();  

                objCompany.Data.CompanyName2 = ConvertData.ConvertToString(eCompanyEntity.CompanyName2);
                objCompany.Data.Address2 = ConvertData.ConvertToString(eCompanyEntity.Address2);
                objCompany.Data.BranchAddress2 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress2);
                objCompany.Data.BranchCompanyName2 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName2);

                objCompany.Data.BranchAddress1 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress1);
                objCompany.Data.CompanyName1 = ConvertData.ConvertToString(eCompanyEntity.CompanyName1);
                objCompany.Data.Address1 = ConvertData.ConvertToString(eCompanyEntity.Address1);
                objCompany.Data.BranchCompanyName1 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName1);

                break;
        }
        
        int nCompanyResult = objCompany.Update(nCompanyLastestID); 

        Languages objLang = new Languages();
        objLang.SetDefaultLanguage(ConvertData.ConvertToInt(ddlDefaultLanguage.SelectedValue));

        int nResult = nCompanyResult * nSettingResult;
        if (nResult > 0)
        {
            Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[Support.GetDefaultLanguage() - 1]);
            string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_SETTING + "&" + Config.MESS_ID + "=" + Config.MESSAGE_UPDATE;
            Response.Redirect(sURL);
        }
        else
        {
            Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[Support.GetDefaultLanguage() - 1]);   
        }
    }

    private eCompany CompanyEntity(int nItemID)
    {
        eCompany entityCompany = new eCompany();
        Company objCompany = new Company();
        objCompany.LoadById(nItemID);
        entityCompany  = objCompany.Data;
        return entityCompany ;
    }

   
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindData(); 
    }
    protected void lnkbtSave_Click(object sender, EventArgs e)
    {
        int nLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        // Updating Setting Data
        Setting objSetting = new Setting();
        DataTable dtbSetting = new DataTable();
        dtbSetting = objSetting.Search();
        int nSettingCount = dtbSetting.Rows.Count;
        int nSettingLastestID = ConvertData.ConvertToInt(dtbSetting.Rows[nSettingCount - 1]["SettingID"]);
        objSetting.Data.MetaKeyword = txtMetaKeyword.Text.Trim();
        objSetting.Data.MetaDescription = txtMetaDescription.Text.Trim();
        int nSettingResult = objSetting.Update(nSettingLastestID);

        // Updating Company Data
        Company objCompany = new Company();
        DataTable dtbCompany = new DataTable();
        dtbCompany = objCompany.Search();
        int nCompanyCount = dtbCompany.Rows.Count;
        int nCompanyLastestID = ConvertData.ConvertToInt(dtbCompany.Rows[nCompanyCount - 1]["CompanyID"]);
        eCompany eCompanyEntity = this.CompanyEntity(nCompanyLastestID);

        objCompany.Data.Website = txtWebsite.Text.Trim();
        objCompany.Data.Tel = txtTel.Text.Trim();
        objCompany.Data.Hotline = txtHotline.Text.Trim();
        objCompany.Data.Fax = txtFax.Text.Trim();
        objCompany.Data.Email = txtEmail.Text.Trim();
        objCompany.Data.Copyright = txtFooter.Text.Trim();
        objCompany.Data.BranchEmail = txtBranchEmail.Text.Trim();
        objCompany.Data.BranchFax = txtBranchFax.Text.Trim();
        objCompany.Data.BranchHotline = txtBranchHotline.Text.Trim();
        objCompany.Data.BranchTel = txtBranchTel.Text.Trim();
        objCompany.Data.YahooAccount1 = txtYahoo1.Text.Trim();
        objCompany.Data.YahooAccount2 = txtYahoo2.Text.Trim();
        objCompany.Data.YahooAccount3 = txtYahoo3.Text.Trim();
        objCompany.Data.YahooAccount4 = txtYahoo4.Text.Trim();
        objCompany.Data.SkypeAccount1 = txtSkype1.Text.Trim();
        objCompany.Data.SkypeAccount2 = txtSkype2.Text.Trim();
        objCompany.Data.SkypeAccount3 = txtSkype3.Text.Trim();
        objCompany.Data.SkypeAccount4 = txtSkype4.Text.Trim();


        switch (nLanguage)
        {
            case 1:
                objCompany.Data.CompanyName1 = txtCompanyName.Text.Trim();
                objCompany.Data.Address1 = txtAddress.Text.Trim();
                objCompany.Data.BranchAddress1 = txtBranchAddress.Text.Trim();
                objCompany.Data.BranchCompanyName1 = txtBranchCompanyName.Text.Trim();

                objCompany.Data.CompanyName2 = ConvertData.ConvertToString(eCompanyEntity.CompanyName2);
                objCompany.Data.Address2 = ConvertData.ConvertToString(eCompanyEntity.Address2);
                objCompany.Data.BranchAddress2 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress2);
                objCompany.Data.BranchCompanyName2 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName2);

                objCompany.Data.CompanyName3 = ConvertData.ConvertToString(eCompanyEntity.CompanyName3);
                objCompany.Data.Address3 = ConvertData.ConvertToString(eCompanyEntity.Address3);
                objCompany.Data.BranchAddress3 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress3);
                objCompany.Data.BranchCompanyName3 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName3);

                break;
            case 2:
                objCompany.Data.CompanyName2 = txtCompanyName.Text.Trim();
                objCompany.Data.Address2 = txtAddress.Text.Trim();
                objCompany.Data.BranchAddress2 = txtBranchAddress.Text.Trim();
                objCompany.Data.BranchCompanyName2 = txtBranchCompanyName.Text.Trim();

                objCompany.Data.CompanyName1 = ConvertData.ConvertToString(eCompanyEntity.CompanyName1);
                objCompany.Data.Address1 = ConvertData.ConvertToString(eCompanyEntity.Address1);
                objCompany.Data.BranchAddress1 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress1);
                objCompany.Data.BranchCompanyName1 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName1);

                objCompany.Data.CompanyName3 = ConvertData.ConvertToString(eCompanyEntity.CompanyName3);
                objCompany.Data.Address3 = ConvertData.ConvertToString(eCompanyEntity.Address3);
                objCompany.Data.BranchAddress3 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress3);
                objCompany.Data.BranchCompanyName3 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName3);

                break;
            case 3:
                objCompany.Data.CompanyName3 = txtCompanyName.Text.Trim();
                objCompany.Data.Address3 = txtAddress.Text.Trim();
                objCompany.Data.BranchAddress3 = txtBranchAddress.Text.Trim();
                objCompany.Data.BranchCompanyName3 = txtBranchCompanyName.Text.Trim();

                objCompany.Data.CompanyName2 = ConvertData.ConvertToString(eCompanyEntity.CompanyName2);
                objCompany.Data.Address2 = ConvertData.ConvertToString(eCompanyEntity.Address2);
                objCompany.Data.BranchAddress2 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress2);
                objCompany.Data.BranchCompanyName2 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName2);

                objCompany.Data.BranchAddress1 = ConvertData.ConvertToString(eCompanyEntity.BranchAddress1);
                objCompany.Data.CompanyName1 = ConvertData.ConvertToString(eCompanyEntity.CompanyName1);
                objCompany.Data.Address1 = ConvertData.ConvertToString(eCompanyEntity.Address1);
                objCompany.Data.BranchCompanyName1 = ConvertData.ConvertToString(eCompanyEntity.BranchCompanyName1);

                break;
        }

        int nCompanyResult = objCompany.Update(nCompanyLastestID);

        Languages objLang = new Languages();
        objLang.SetDefaultLanguage(ConvertData.ConvertToInt(ddlDefaultLanguage.SelectedValue));

        int nResult = nCompanyResult * nSettingResult;
        if (nResult > 0)
        {
            Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[Support.GetDefaultLanguage() - 1]);
            string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_SETTING + "&" + Config.MESS_ID + "=" + Config.MESSAGE_UPDATE;
            Response.Redirect(sURL);
        }
        else
        {
            Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[Support.GetDefaultLanguage() - 1]);
        }
    }
    protected void ddlDefaultLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}
